﻿using demo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConstructorClass
{
    /// <summary>
    /// 创建类对象构造器
    /// </summary>
    [ReflectAble]
    public class Constructor:ActionItem
    {
        public ConstructorInfo _constructorInfo;

        public override bool Initilize()
        {
            try
            {
                string fileName = Environment.CurrentDirectory + "\\" + Dll;
                var curAssembly = Assembly.LoadFile(fileName);
                var type = curAssembly.GetType(TypeName);

                _constructorInfo = type.GetConstructor(ParaTypes.Select(Type.GetType).ToArray());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                Em.GetEvent<DebugMessage>().Publish($"初始化{ItemName}时出现异常:{e.Message}");
            }
            return true;
        }

        protected override bool RunMethod()
        {
            try
            {
                //var vars = GlobalVars.Ins.Vars[GlobalVars.FlowVars];
                Initilize();

                #region 参数数组对象

                var objs = new List<object>();
                for (int i = 0; i < Parms.Count; i++)
                {
                    var item = Parms[i];
                    object temp = null;
                    if (!item.InputOrSelect)
                    {
                        temp = GlobalVars.Ins.GetVar(item.VarGuid).Value;

                    }
                    else
                    {
                        var valueType = Type.GetType(ParaTypes[i]);
                        if (valueType == typeof(int)) temp = Convert.ToInt32(item.Value);
                        else if (valueType == typeof(uint)) temp = Convert.ToUInt32(item.Value);
                        else if (valueType == typeof(short)) temp = Convert.ToInt16(item.Value);
                        else if (valueType == typeof(ushort)) temp = Convert.ToUInt16(item.Value);
                        else if (valueType == typeof(long)) temp = Convert.ToInt64(item.Value);
                        else if (valueType == typeof(ulong)) temp = Convert.ToUInt64(item.Value);
                        else if (valueType == typeof(Single)) temp = Convert.ToSingle(item.Value);
                        else if (valueType == typeof(double)) temp = Convert.ToDouble(item.Value);
                        else
                        {
                            temp = item.Value;
                        }
                    }
                    if (temp != null) objs.Add(temp);
                }

                #endregion

                #region 执行构造函数

                object[] objArray = null;
                if (objs.Count > 0)
                {
                    objArray = objs.ToArray();
                }

                var result = _constructorInfo.Invoke(objArray);

                #endregion

                #region 返回值赋值

                if (!ReturnType.Contains("Void") && !string.IsNullOrEmpty(ReturnObjName))
                {
                    var varinfo = GlobalVars.Ins.GetVar(ReturnObjGuid);
                    if (varinfo != null) varinfo.Value = result;

                }

                #endregion


            }
            catch (Exception e)
            {
                Em.GetEvent<DebugMessage>().Publish($"运行{ItemName}时出现异常:{e.Message}");
                return false;
            }

            return true;
        }
    }
}
